﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HongLingJin
{
    public partial class IntradayFrom : Form
    {
        private VisitorInfoDAL viDAL;
        int pageSize = 0;     //每页显示行数
        int nMax = 0;         //总记录数
        int pageCount = 0;    //页数＝总记录数/每页显示行数
        int pageCurrent = 0;   //当前页号
        int nCurrent = 0;      //当前记录行
        List<VisitorInfo> vis = new List<VisitorInfo>();
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        public IntradayFrom()
        {
            InitializeComponent();
        }

        private void IntradayFrom_Load(object sender, EventArgs e)
        {
            InitDataSet();
        }

        private void InitDataSet()
        {
            viDAL = new VisitorInfoDAL();
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            
            if (tabControl1.SelectedIndex == 0)
            {
                int start = (int)(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00") - startTime).TotalSeconds;
                int end = (int)(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59") - startTime).TotalSeconds;
                vis = viDAL.selectVisitorInfo(start,end,tabControl1.SelectedIndex);
                nMax = viDAL.VisitorInfoCount(start, end, tabControl1.SelectedIndex);
            }
            else
            {
                int trimester = 90 * 24 * 60 * 60;
                int start = (int)(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00") - startTime).TotalSeconds - trimester;
                vis = viDAL.selectVisitorInfo(start,0,tabControl1.SelectedIndex);
                nMax = viDAL.VisitorInfoCount(start, 0, tabControl1.SelectedIndex);
            }
            
            pageSize = 20;      //设置页面行数
            lbCount.Text = nMax.ToString();
            lbPageCount.Text = pageSize.ToString();
            pageCount = (nMax / pageSize);    //计算出总页数

            if ((nMax % pageSize) > 0) pageCount++;

            pageCurrent = 1;    //当前页数从1开始
            nCurrent = 0;       //当前记录数从0开始
            LoadData();
        }

        private void LoadData()
        {
            int nStartPos = 0;   //当前页面开始记录行
            int nEndPos = 0;     //当前页面结束记录行
            if (pageCount == 0)
                nEndPos = 0;
            else if (pageCurrent == pageCount)
                nEndPos = nMax;
            else
                nEndPos = pageSize * pageCurrent;

            nStartPos = nCurrent;

            //lblPageCount2.Text = pageCount.ToString();
            tbCurrentPage.Text = Convert.ToString(pageCurrent);
            lbPages.Text = pageCount.ToString();
            if (tabControl1.SelectedIndex == 0)
            {
                this.lvToday.Items.Clear();
            }
            else
            {
                this.lvHistory.Items.Clear();
            }
            
            //从元数据源复制记录行
            for (int i = nStartPos; i < nEndPos; i++)
            {
                imageList1 = new ImageList();
                imageList1.ImageSize = new Size(80, 60);
                if (tabControl1.SelectedIndex == 0)
                {
                    lvToday.SmallImageList = imageList1;
                }
                else
                {
                    lvHistory.SmallImageList = imageList1;
                }
                
                ListViewItem lv = new ListViewItem();
                //lv.Text = vi.Id;
                lv.ImageIndex = 0;
                imageList1.Images.Add(Image.FromFile(vis[i].File));

                lv.SubItems.Add(vis[i].Name);
                lv.SubItems.Add(vis[i].Visitee_name);
                lv.SubItems.Add(vis[i].Identity_number);
                switch (vis[i].Visit_purpose)
                {
                    case "0":
                        lv.SubItems.Add("其他");
                        break;
                    case "1":
                        lv.SubItems.Add("商务洽谈");
                        break;
                    case "2":
                        lv.SubItems.Add("面试");
                        break;
                    case "3":
                        lv.SubItems.Add("亲友会见");
                        break;
                    case "4":
                        lv.SubItems.Add("外卖快递");
                        break;
                }

                lv.SubItems.Add(startTime.AddSeconds(vis[i].createDate).ToString());
                if (tabControl1.SelectedIndex == 0)
                {
                    lvToday.Items.Add(lv);
                }
                else
                {
                    lvHistory.Items.Add(lv);
                }
                
                
                nCurrent++;
            }
     
        }

        private void btnPageUp_Click(object sender, EventArgs e)
        {
            pageCurrent--;
            if (pageCurrent <= 0)
            {
                MessageBox.Show("已经是第一页，请点击“下一页”查看！");
                pageCurrent++;
                return;
            }
            else
            {
                nCurrent = pageSize * (pageCurrent - 1);
            }

            LoadData();
        }

        private void btnPageDown_Click(object sender, EventArgs e)
        {
            pageCurrent++;
            if (pageCurrent > pageCount)
            {
                MessageBox.Show("已经是最后一页，请点击“上一页”查看！");
                pageCurrent--;
                return;
            }
            else
            {
                nCurrent = pageSize * (pageCurrent - 1);
            }
            LoadData();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitDataSet();
        }
    }
}
